package Pages.System;

import JDBC.DBUtil;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ResourceBundle;

import static Data.Util.getCount;

/**
 * description:
 * author: 柳胜酉
 * 2022/7/8
 */
public class SRegisterPage extends JFrame {
    private JPanel dialogPane;
    private JPanel contentPanel;
    private JPanel panel6;
    private JLabel label5;
    private JPanel panel1;
    private JLabel label1;
    private JTextField textField1;
    private JPanel panel2;
    private JLabel label2;
    private JTextField textField2;
    private JPanel panel3;
    private JLabel label3;
    private JPasswordField passwordField1;
    private JPanel panel5;
    private JLabel label4;
    private JPasswordField passwordField2;
    private JPanel panel4;
    private JRadioButton radioButton1;
    private JRadioButton radioButton2;
    private JRadioButton radioButton3;
    private JPanel buttonBar;
    private JButton okButton;
    private JButton cancelButton;
    private String count = null;
    public SRegisterPage() {
        initComponents();
    }

    private void initComponents() {
        ResourceBundle bundle = ResourceBundle.getBundle("P0704");
        dialogPane = new JPanel();
        contentPanel = new JPanel();
        panel6 = new JPanel();
        label5 = new JLabel();
        panel1 = new JPanel();
        label1 = new JLabel();
        textField1 = new JTextField();
        panel2 = new JPanel();
        label2 = new JLabel();
        textField2 = new JTextField();
        panel3 = new JPanel();
        label3 = new JLabel();
        passwordField1 = new JPasswordField();
        panel5 = new JPanel();
        label4 = new JLabel();
        passwordField2 = new JPasswordField();
        panel4 = new JPanel();
        radioButton1 = new JRadioButton();
        radioButton2 = new JRadioButton();
        radioButton3 = new JRadioButton();
        buttonBar = new JPanel();
        okButton = new JButton();
        cancelButton = new JButton();

        //======== this ========
        setTitle("\u6ce8\u518c\u9875");
        setMaximizedBounds(new Rectangle(400, 400, 330, 420));
        setLocationByPlatform(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);
        setBackground(UIManager.getColor("Button.darkShadow"));
        var contentPane = getContentPane();
        contentPane.setLayout(new BorderLayout());

        //======== dialogPane ========
        {
            dialogPane.setBorder(new EmptyBorder(12, 12, 12, 12));
            dialogPane.setBackground(SystemColor.inactiveCaption);
            dialogPane.setLayout(new BorderLayout());

            //======== contentPanel ========
            {
                contentPanel.setBackground(SystemColor.inactiveCaption);
                contentPanel.setLayout(new GridLayout(6, 1));

                //======== panel6 ========
                {
                    panel6.setBackground(SystemColor.activeCaption);
                    panel6.setLayout(new GridLayout(1, 1));

                    //---- label5 ----
                    label5.setText(bundle.getString("RegisterPage.label5.text"));
                    label5.setHorizontalAlignment(SwingConstants.CENTER);
                    label5.setFont(label5.getFont().deriveFont(label5.getFont().getSize() + 20f));
                    label5.setForeground(Color.white);
                    panel6.add(label5);
                }
                contentPanel.add(panel6);

                //======== panel1 ========
                {
                    panel1.setBackground(SystemColor.inactiveCaption);
                    panel1.setLayout(new GridLayout(1, 2));

                    //---- label1 ----
                    label1.setText(bundle.getString("RegisterPage.label1.text"));
                    label1.setFont(label1.getFont().deriveFont(label1.getFont().getSize() + 15f));
                    label1.setBackground(new Color(153, 204, 255));
                    label1.setForeground(Color.darkGray);
                    label1.setIcon(new ImageIcon(getClass().getResource("/image/xingming.png")));
                    panel1.add(label1);

                    //---- textField1 ----
                    textField1.setFont(textField1.getFont().deriveFont(textField1.getFont().getSize() + 5f));
                    panel1.add(textField1);
                }
                contentPanel.add(panel1);

                //======== panel2 ========
                {
                    panel2.setBackground(SystemColor.inactiveCaption);
                    panel2.setLayout(new GridLayout(1, 2));

                    //---- label2 ----
                    label2.setText(bundle.getString("RegisterPage.label2.text"));
                    label2.setFont(label2.getFont().deriveFont(label2.getFont().getSize() + 14f));
                    label2.setBackground(new Color(153, 204, 255));
                    label2.setForeground(Color.darkGray);
                    label2.setIcon(new ImageIcon(getClass().getResource("/image/denglu.png")));
                    panel2.add(label2);

                    //---- textField2 ----
                    textField2.setFont(textField2.getFont().deriveFont(textField2.getFont().getSize() + 5f));
                    panel2.add(textField2);
                }
                contentPanel.add(panel2);

                //======== panel3 ========
                {
                    panel3.setBackground(SystemColor.inactiveCaption);
                    panel3.setLayout(new GridLayout(1, 2));

                    //---- label3 ----
                    label3.setText(bundle.getString("RegisterPage.label3.text"));
                    label3.setFont(label3.getFont().deriveFont(label3.getFont().getSize() + 15f));
                    label3.setForeground(Color.darkGray);
                    label3.setIcon(new ImageIcon(getClass().getResource("/image/mima.png")));
                    panel3.add(label3);

                    //---- passwordField1 ----
                    passwordField1.setFont(passwordField1.getFont().deriveFont(passwordField1.getFont().getSize() + 5f));
                    panel3.add(passwordField1);
                }
                contentPanel.add(panel3);

                //======== panel5 ========
                {
                    panel5.setBackground(SystemColor.inactiveCaption);
                    panel5.setLayout(new GridLayout(1, 2));

                    //---- label4 ----
                    label4.setText(bundle.getString("RegisterPage.label4.text"));
                    label4.setFont(label4.getFont().deriveFont(label4.getFont().getSize() + 15f));
                    label4.setBackground(new Color(153, 204, 255));
                    label4.setForeground(Color.darkGray);
                    label4.setIcon(new ImageIcon(getClass().getResource("/image/querenmima_1.png")));
                    panel5.add(label4);

                    //---- passwordField2 ----
                    passwordField2.setFont(passwordField2.getFont().deriveFont(passwordField2.getFont().getSize() + 5f));
                    panel5.add(passwordField2);
                }
                contentPanel.add(panel5);

//                //======== panel4 ========
//                {
//                    panel4.setBackground(SystemColor.inactiveCaption);
//                    panel4.setLayout(new GridLayout(1, 1));
//
//                    //---- radioButton1 ----
//                    radioButton1.setText(bundle.getString("RegisterPage.radioButton1.text"));
//                    radioButton1.setFont(radioButton1.getFont().deriveFont(radioButton1.getFont().getSize() + 5f));
//                    radioButton1.setBackground(SystemColor.inactiveCaption);
//                    radioButton1.setForeground(Color.darkGray);
//                    panel4.add(radioButton1);
//
//                    //---- radioButton2 ----
//                    radioButton2.setText(bundle.getString("RegisterPage.radioButton2.text"));
//                    radioButton2.setFont(radioButton2.getFont().deriveFont(radioButton2.getFont().getSize() + 5f));
//                    radioButton2.setBackground(SystemColor.inactiveCaption);
//                    radioButton2.setForeground(Color.darkGray);
//                    panel4.add(radioButton2);
//
//                    //---- radioButton3 ----
//                    radioButton3.setText(bundle.getString("RegisterPage.radioButton3.text"));
//                    radioButton3.setFont(radioButton3.getFont().deriveFont(radioButton3.getFont().getSize() + 5f));
//                    radioButton3.setBackground(SystemColor.inactiveCaption);
//                    radioButton3.setForeground(Color.darkGray);
//                    panel4.add(radioButton3);
//                }
//                contentPanel.add(panel4);
            }
            dialogPane.add(contentPanel, BorderLayout.CENTER);

            //======== buttonBar ========
            {
                buttonBar.setBorder(new EmptyBorder(12, 0, 0, 0));
                buttonBar.setBackground(SystemColor.inactiveCaption);
                buttonBar.setLayout(new GridBagLayout());
                ((GridBagLayout)buttonBar.getLayout()).columnWidths = new int[] {0, 85, 80};
                ((GridBagLayout)buttonBar.getLayout()).columnWeights = new double[] {1.0, 0.0, 0.0};

                //---- okButton ----
                okButton.setText(bundle.getString("RegisterPage.okButton.text_2"));
                okButton.setFont(okButton.getFont().deriveFont(okButton.getFont().getSize() + 2f));
                okButton.setForeground(Color.black);
                okButton.setBackground(UIManager.getColor("Button.darkShadow"));
                buttonBar.add(okButton, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0,
                        GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                        new Insets(0, 0, 0, 5), 0, 0));

                //---- cancelButton ----
                cancelButton.setText(bundle.getString("RegisterPage.cancelButton.text_2"));
                cancelButton.setFont(cancelButton.getFont().deriveFont(cancelButton.getFont().getSize() + 2f));
                cancelButton.setForeground(Color.black);
                cancelButton.setBackground(UIManager.getColor("Button.darkShadow"));
                buttonBar.add(cancelButton, new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0,
                        GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                        new Insets(0, 0, 0, 0), 0, 0));
            }
            dialogPane.add(buttonBar, BorderLayout.SOUTH);
        }
        contentPane.add(dialogPane, BorderLayout.CENTER);
        setSize(330, 420);
        setLocationRelativeTo(null);

//        //---- buttonGroup1 ----
//        var buttonGroup1 = new ButtonGroup();
//        buttonGroup1.add(radioButton1);
//        buttonGroup1.add(radioButton2);
//        buttonGroup1.add(radioButton3);

        okButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                count = getCount(textField1.getText());
                if(checkPwd1ByPwd2(new String(passwordField1.getPassword()),new String(passwordField2.getPassword()))){
                    if(DBUtil.insertData(textField2.getText(),textField1.getText(),new String(passwordField1.getPassword()))){
                        JOptionPane.showMessageDialog(null, "注册成功！", "注册提示", JOptionPane.INFORMATION_MESSAGE);
                        dispose();
                        new SHomePage(count).setVisible(true);
                    }else {
                        JOptionPane.showMessageDialog(null, "注册失败！", "注册提示", JOptionPane.INFORMATION_MESSAGE);
                    }
                }else {
                    JOptionPane.showMessageDialog(null, "两次输入密码不一致！", "注册提示", JOptionPane.INFORMATION_MESSAGE);
                }
            }
        });

        cancelButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
                new LoginPage().setVisible(true);
            }
        });
    }
    public static boolean checkPwd1ByPwd2(String pwd1, String pwd2) {
        boolean flag =false;
        if(pwd1.equals(pwd2)){
            flag = true;
        }
        return flag;
    }

    public static void main(String[] args) {
        new SRegisterPage().setVisible(true);
    }
}
